package com.bracketbird.client.gui.club.tournament;


import com.bracketbird.client.gui.club.tournament.rtc.*;
import com.bracketbird.client.gui.club.tournament.teams.*;
import com.bracketbird.client.model.*;
import com.bracketbird.client.model.tournament.*;
import com.bracketbird.clientcore.gui.*;
import com.bracketbird.clientcore.style.*;
import com.bracketbird.clientcore.util.*;

/**
 *
 */
public class TeamsTableRow extends HorizontalComponent {

    private static TextLayout focusLayout = new TextLayout().sizeH3().bold().add(P.BACKGROUND_WHITE).colorBlack().border(2).borderColor(new P(Name.COLOR, "#29ABE2"));

    private static TextLayout focusLostLayout = new TextLayout().sizeNormal().add(P.BACKGROUND_WHITE).border(2).borderColor(P.BACKGROUND_WHITE).add(Name.COLOR, "#5C5C5C");

    private static TextLayout passivLayout = new TextLayout().sizeNormal().add(P.BACKGROUND_F5).border(2).borderColor(P.BACKGROUND_F5).add(Name.COLOR, "#5C5C5C");


    private PlayingTeam team;
    private boolean hasFocus = false;
    private EditOnlyTableData columnTeamName;
    private EditOnlyTableData columnComment;
    private EditOnlyTableData columnSeeding;

    private EditOnlyTableData[] allCells;

    private TableNotifier tableNotifier;

    public TeamsTableRow(PlayingTeam team, TableNotifier tableNotifier) {
        super();
        this.team = team;
        this.tableNotifier = tableNotifier;
        this.add(getColumnTeamName(), new TextLayout("22px", "500px"));
        this.add(getColumnComment(), new TextLayout("22px", "200px"));
        this.add(getColumnSeeding(), new TextLayout("22px", "100%").alignRight());


        allCells = new EditOnlyTableData[]{getColumnTeamName(), getColumnComment(), getColumnSeeding()};

        team.addTeamsListener(new TournamentListener<TournamentTeamEvent>() {
            public void onChange(TournamentTeamEvent event) {
                TeamsTableRow.this.team = event.getTeam();
                if (!event.isClientEvent() && event.getAction() == TournamentTeamEvent.Action.update) {
                    TeamsTableRow.this.team = event.getTeam();
                    refreshRowValues();
                }
            }
        });
        refreshRowValues();
        paintFocusLost();
    }

    private void refreshRowValues() {
        getColumnTeamName().setText(getTeam().getTeamName());
        getColumnComment().setText(getTeam().getInfo());
        getColumnSeeding().setText(getTeam().getSeeding() != null ? String.valueOf(getTeam().getSeeding()) : "");
    }


    public EditOnlyTableData getColumnTeamName() {
        if (columnTeamName == null) {
            columnTeamName = new EditOnlyTableData(new TableRowNotifier() {
                public void commit(String value) {
                    if (!StringUtil.equals(team.getTeamName(), value)) {
                        team.setTeamName(value);
                        RTC.getInstance().updateTeam(team, team.createVO(), true);
                    }
                }

                public void up() {
                    tableNotifier.up(TeamsTableRow.this, 0);
                }

                public void down() {
                    tableNotifier.down(TeamsTableRow.this, 0);
                }

                public void right() {
                    getColumnComment().setFocus();
                }

                public void left() {
                    //ignore
                }

                public void deleteRow() {
                    delete();
                }

                public void focusLost() {
                    paintFocusLost();
                }

                public void focusGained() {
                    paintFocus(columnTeamName);
                }

            });
        }
        return columnTeamName;
    }


    public EditOnlyTableData getColumnComment() {
        if (columnComment == null) {
            columnComment = new EditOnlyTableData(new TableRowNotifier() {
                public void commit(String value) {
                    if (!StringUtil.equals(team.getInfo(), value)) {
                        team.setInfo(value);
                        RTC.getInstance().updateTeam(team, team.createVO(), true);
                    }
                }

                public void up() {
                    tableNotifier.up(TeamsTableRow.this, 1);
                }

                public void down() {
                    tableNotifier.down(TeamsTableRow.this, 1);
                }

                public void right() {
                    getColumnSeeding().setFocus();
                }

                public void left() {
                    getColumnTeamName().setFocus();
                }

                public void deleteRow() {
                    //ignore
                }

                public void focusLost() {
                    paintFocusLost();
                }

                public void focusGained() {
                    paintFocus(columnComment);
                }
            });
        }
        return columnComment;
    }


    public EditOnlyTableData getColumnSeeding() {
        if (columnSeeding == null) {
            columnSeeding = new EditOnlyTableData(new TableRowNotifier() {
                public void commit(String value) {
                    try {
                        if(!team.hasSeeding() && StringUtil.isEmpty(value)){
                            return;
                        }
                        Integer seed = Integer.valueOf(value);
                        if (!EqualsUtil.equals(team.getSeeding(), seed)) {
                            team.setSeeding(seed);
                            RTC.getInstance().updateTeam(team, team.createVO(), true);
                        }
                    }
                    catch (Exception e) {
                        //TODO
                        columnSeeding.setFocus();
                    }
                }

                public void up() {
                    tableNotifier.up(TeamsTableRow.this, 2);
                }

                public void down() {
                    tableNotifier.down(TeamsTableRow.this, 2);
                }

                public void right() {
                    //ignore
                }

                public void left() {
                    getColumnComment().setFocus();
                }

                public void deleteRow() {
                    //ignore
                }

                public void focusLost() {
                    paintFocusLost();
                }

                public void focusGained() {
                    paintFocus(columnSeeding);
                }
            });
        }
        return columnSeeding;
    }


    private void paintFocus(EditOnlyTableData toFocus) {
        for (EditOnlyTableData cell : allCells) {
            if(cell.equals(toFocus)){
                StyleIt.add(toFocus, focusLayout);
            }
            else{
                StyleIt.add(cell, passivLayout);
            }
        }
        hasFocus = true;
    }

    private void paintFocusLost() {
        StyleIt.add(getColumnTeamName(), focusLostLayout);
        StyleIt.add(getColumnComment(), focusLostLayout);
        StyleIt.add(getColumnSeeding(), focusLostLayout);
        tableNotifier.focusLost(this);
        hasFocus = false;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        TeamsTableRow that = (TeamsTableRow) o;

        if (!team.equals(that.team)) {
            return false;
        }

        return true;
    }

    private void delete() {
        RTC.getInstance().deleteTeam(team, true);
    }

    @Override
    public int hashCode() {
        return team.hashCode();
    }


    public PlayingTeam getTeam() {
        return team;
    }


    public void setFocus(boolean focus) {
        getColumnTeamName().setFocus();
    }


    public boolean hasFocus() {
        return hasFocus;
    }
}

